import axios from 'axios'


export const domain = '/api'
export const domainV3 = '/api/v3'

const XToken = '351c6445-31a5-4299-8125-bfe349b670ef'
process.env.NODE_ENV === 'development' && (axios.defaults.headers.XToken = XToken)

const instance = axios.create({
    timeout: 60000,
    headers: {
        'Content-Type': 'application/json;charset=UTF-8',
		'Cache-Control': 'no-cache,no-store,must-revalidate,max-age=-1,private',
		Expires: -1 
    }
})

instance.interceptors.request.use(
    config => {
        return config
    },
    err => {
        return err
    }
)

instance.interceptors.response.use(
    response => {
        if(response.data && response.data.error) {
            if (response.data.error === 1000 || response.data.error.msg.includes('请先登录')) {
                location.href = '/login'
            }
        }

        return response.data
    },
    err => {
        if (err && err.response) {
            switch (err.response.status) {
                case 400:
                    break
            }
        }
        if (err.toString().includes('Error: timeout of')) {
            err.timeout = true
        }
        if (err.toString().includes('Network Error') || err.toString().includes('Request failed')) {
            err.NetworkError = true
        }
    
        return err
    }
)

export default instance